package com.example.calendar.data.repository

import com.example.calendar.data.local.dao.EventDao
import com.example.calendar.data.model.CalendarEvent
import kotlinx.coroutines.flow.Flow
import org.threeten.bp.LocalDateTime
import javax.inject.Inject
import javax.inject.Singleton

/**
 * Interface for event repository operations
 */
interface EventRepository {
    suspend fun addEvent(event: CalendarEvent): String
    suspend fun updateEvent(event: CalendarEvent)
    suspend fun deleteEvent(eventId: String)
    fun getEventById(eventId: String): Flow<CalendarEvent?>
    fun getAllEvents(): Flow<List<CalendarEvent>>
    fun getAllDayEvents(): Flow<List<CalendarEvent>>
    fun getEventsForDate(date: LocalDateTime): Flow<List<CalendarEvent>>
    fun getEventsBetweenDates(start: LocalDateTime, end: LocalDateTime): Flow<List<CalendarEvent>>
    fun getEventsByCategory(categoryId: String): Flow<List<CalendarEvent>>
    fun searchEvents(query: String): Flow<List<CalendarEvent>>
    fun getEventCountForDate(date: LocalDateTime): Flow<Int>
}

/**
 * Implementation of EventRepository that works with the Room database
 */
@Singleton
class EventRepositoryImpl @Inject constructor(
    private val eventDao: EventDao
) : EventRepository {
    
    override suspend fun addEvent(event: CalendarEvent): String {
        eventDao.insertEvent(event)
        return event.id
    }
    
    override suspend fun updateEvent(event: CalendarEvent) {
        eventDao.updateEvent(event)
    }
    
    override suspend fun deleteEvent(eventId: String) {
        eventDao.softDeleteEvent(eventId)
    }
    
    override fun getEventById(eventId: String): Flow<CalendarEvent?> {
        return eventDao.getEventById(eventId)
    }
    
    override fun getAllEvents(): Flow<List<CalendarEvent>> {
        return eventDao.getAllEvents()
    }
    
    override fun getAllDayEvents(): Flow<List<CalendarEvent>> {
        return eventDao.getAllDayEvents()
    }
    
    override fun getEventsForDate(date: LocalDateTime): Flow<List<CalendarEvent>> {
        return eventDao.getEventsForDate(date)
    }
    
    override fun getEventsBetweenDates(start: LocalDateTime, end: LocalDateTime): Flow<List<CalendarEvent>> {
        return eventDao.getEventsBetweenDates(start, end)
    }
    
    override fun getEventsByCategory(categoryId: String): Flow<List<CalendarEvent>> {
        return eventDao.getEventsByCategory(categoryId)
    }
    
    override fun searchEvents(query: String): Flow<List<CalendarEvent>> {
        return eventDao.searchEvents(query)
    }
    
    override fun getEventCountForDate(date: LocalDateTime): Flow<Int> {
        return eventDao.getEventCountForDate(date)
    }
} 